package com.chazz.servlet;

import com.alibaba.fastjson.JSONArray;
import com.chazz.entity.Goods;
import com.chazz.service.CategoryidService;
import com.chazz.service.GoodsService;
import com.chazz.service.impl.CategoryidServiceImpl;
import com.chazz.service.impl.GoodsServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/goodsServlet.do")
public class GoodsServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("appliction/json;charset=UTF-8");
        if (req.getParameter("method").equals("categorySearch")) {
            GoodsService goodsService = new GoodsServiceImpl();
            List<Goods> goodsList = goodsService.findGoodsList(req.getParameter("categoryname"));
            String toJSON = JSONArray.toJSONString(goodsList);
            System.out.println(toJSON);
            PrintWriter out = resp.getWriter();
            out.write(toJSON);

        }
    }
}
